<?php

namespace App\Http\Controllers\Api\V1\Game;

use App\Game\sc;
use App\Models\Conf\Site;
use App\Models\Def\MainGamePlat;
use App\Models\Inf\Player;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Dingo\Api\Routing\Helpers;
use Illuminate\Support\Facades\Cache;

class ScController extends Controller
{
    use Helpers;

    protected $cacheKey;

    /*http://www.betapi.co/api/game/sc/login   //验证用户token
    http://www.betapi.co/api/game/sc/recharge  //验证资金token*/
    public function login(Request $request)
    {
        $token = $request->token ?? null;
        if ($token) {
            $data = json_decode(decrypt($token), true);
            $this->cacheKey = $data['username'];
            if (Cache::get($this->cacheKey) === $token) {
                if (array_key_exists('site_username', $data)) {
                    $site = Site::where('username', $data['site_username'])->first();
                    $plat = MainGamePlat::where('main_game_plat_code', $data['plat'])->first();
                    $player = Player::where([
                            ['site_id', '=', $site->id],
                            ['username', '=', $data['username']],
                            ['password', '=', $data['password']],
                            ['main_game_plats_id', '=', $plat->id],
                        ]
                    )->first();
                    if ($player) {
                        return $this->response->array([
                                'result' => 0,
                                'data' => [
                                    'thirdpartyUid' => $data['username'],
                                    'nickname' => $data['username'],
                                    'currency' => 'CNY',
                                    'userLang' => 'zh-cn',
                                    'country' => 'CN',
                                    'lastIp' => $data['ip'],
                                    'isTester' => 0,
                                ]
                            ]
                        );
                    } else {
                        return $this->response->array([
                                'result' => 422,
                                'data' => [
                                    'message' => '账户密码验证失败'
                                ]
                            ]
                        );
                    }
                }
            } else {
                return $this->response->array([
                        'result' => 401,
                        'data' => [
                            'message' => 'token 过期或无效'
                        ]
                    ]
                );
            }
        } else {
            return $this->response->array([
                    'result' => 500,
                    'data' => [
                        'message' => 'token 参数不存在'
                    ]
                ]
            );
        }
    }


    public function recharge(Request $request)
    {
        //$sc = new sc();
        //$token = $sc->rechargeToken('ADMINX', 'jdb198', 10, 'in');

        $token = $request->token ?? null;
        if (!empty($token)) {
            $data = json_decode(decrypt($token), true);
            \Log::info($data);
            $this->cacheKey = $data['username'] . '_transfer';
            if (Cache::get($this->cacheKey) == $token) {
                if (array_key_exists('site_username', $data)) {
                    $site = Site::where('username', $data['site_username'])->with([
                            'MainGamePlat' => function ($query) use ($data) {
                                $query->where('main_game_plat_code', $data['plat']);
                            }
                        ]
                    )->first()->toArray();

                    if ($data['op'] == 'in') {
                        if ($site['main_game_plat'][0]['pivot']['score'] >= $data['price']) {
                            return $this->scResponse(0);
                        } else {
                            return $this->scResponse(422, '商户平台余额不足');
                        }
                    } elseif ($data['op'] == 'out') {
                        return $this->scResponse(0);
                    } else {
                        return $this->scResponse(422, '验证参数op不存在');
                    }
                } else {
                    return $this->scResponse(422, '验证参数site_username不存在');
                }
            } else {
                return $this->scResponse(401, 'token过期或无效');
            }
        } else {
            return $this->scResponse(500, 'token参数不存在');
        }

    }


    public function scResponse($status, $message = '')
    {
        \Log::info([
                'result' => $status,
                'message' => $message
            ]
        );
        Cache::forget($this->cacheKey);
        return $this->response->array([
                'result' => $status,
                'message' => $message
            ]
        );
    }
}
